<?php
class artCategoryModel extends Model{
	private $tbl = 'article_category';
	private $rule = array(
			'name'=>array('required'=>true,'min'=>1,'max'=>15),
			'pid'=>array('num'=>true),
			//'sort'=>array('num'=>true)
			);

	public function __construct(){
		parent::__construct($this->tbl);
	}

	public function add($data){
		if( $this->rule && !$this->valid($data, $this->rule) ) return false;
		return $this->value($data)->insert();
	}

	public function edit($id, $data){
		if( empty($id) || !is_int($id) ) return false;
		if( !$this->valid($data, $this->rule) ) return false;
		if( $data['pid']==$id ) return false;
		return $this->where("id=$id")->value($data)->update();
	}

	public function del($id){
		$id = intval($id);
		if( !is_int($id) || $id<0 ) return false;
		return $this->where("id=$id")->delete();
	}

	public function get_categories($where=""){
		$arr = $this->where($where)->order('sort,id')->get();
		if( !empty($arr) ){
			foreach($arr as $k=>$v){
				$arr[$k]['parent'] = '无';
				foreach( $arr as $k2=>$v2 ){
					if( $v['pid'] == $v2['id'] ){
						$arr[$k]['parent'] = $v2['name'];
					}
				}
			}
		}
		return $arr;
	}
}
